package ystp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ystp.model.Ysuser;
import ystp.service.*;
import ystp.support.JsonResult;
import ystp.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tongkeyaopin")
public class TongkeyaopinController {
    @Resource
    TongkeyaopinService ddService;

    @Resource
    YsrenyuanfenpeiService ddYsrenyuanfenpeiService;

    @Resource
    YsuserService ddYsuserService;

    @Resource
    TongkeyaopinjieguoService ddTongkeyaopinjieguoService;

    public Logger log = LoggerFactory.getLogger(TongkeyaopinController.class);

    // 查列表
    @RequestMapping("findtongkeyaopinlist")
    public JsonResult<?> findTongkeyaopinlist(@RequestParam("xiangmuid") String ppXiangmuId, @RequestParam("pageindex") int ppPageIndex,
                                                    @RequestParam("pagesize") int ppPageSize){
        try {

            List<Map<String,Object>> mmList = ddService.findTongkeyaopinByXiangmuId(ppXiangmuId,ppPageIndex,ppPageSize);
            Integer mmCount = ddService.getTongkeCount(ppXiangmuId,"","","","","");
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("TongkeyaopinList", mmList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("tongkeyaopin/findtongkeyaopinlist:error",e);
            return JsonResult.getErrorResult("tongkeyaopin/findtongkeyaopinlist:error " + e.getMessage());
        }

    }


    @RequestMapping("findBiaojuelist")
    public JsonResult<?> findBiaojuelist(@RequestParam("xiangmuid") String ppxiangmuId,
                                         @RequestParam("userid") String ppuserId,

                                         @RequestParam("drugname") String ppdrugname,
                                         @RequestParam("jiyao") String ppjiyao,
                                         @RequestParam("yizhipingjia") String ppyizhipingjia,
                                         @RequestParam("jicaiyao") String ppjicaiyao,
                                         @RequestParam("yuxuanyijian") String ppyuxuanyijian,

                                         @RequestParam("pageindex") int ppPageIndex,
                                         @RequestParam("pagesize") int ppPageSize) {
        try {
            List<Map<String,Object>> mmList = ddService.findResultByXiangmuIdAndUserid(ppxiangmuId,ppuserId,
                    ppdrugname,ppjiyao,ppyizhipingjia,ppjicaiyao,ppyuxuanyijian,
                    ppPageIndex,ppPageSize);
            Integer mmCount = ddService.getTongkeCount(ppxiangmuId,ppdrugname,ppjiyao,ppyizhipingjia,ppjicaiyao,ppyuxuanyijian);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("TongkeyaopinList", mmList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("tongkeyaopin/findBiaojuelist:error",e);
            return JsonResult.getErrorResult("tongkeyaopin/findBiaojuelist:error " + e.getMessage());
        }
    }


    @RequestMapping("getjindulist")
    public JsonResult<?> getjindulist(@RequestParam("xiangmuid") String ppxiangmuId){
        try{
            List<Map<String,Object>> mmJindulist = new ArrayList<>();
            List<Map<String, Object>> mmUserList=ddYsrenyuanfenpeiService.findRenyuanFenpeiByXiangmuId(ppxiangmuId);
            for(Map<String, Object> ysrenyuanfenpei:mmUserList){
                Map<String, Object> mmMap = new HashMap<String, Object>();
                String mmUserId=ysrenyuanfenpei.get("userid").toString();
                Ysuser mmYsuser=ddYsuserService.findOne(mmUserId);
                String mmUserName=mmYsuser.getUsername();

                Integer mmpinguishu=ddTongkeyaopinjieguoService.getTongKeResultCount(ppxiangmuId,mmUserId,"1");
                int mmjicaiyaoshu=ddTongkeyaopinjieguoService.getJiCaiAgreedCount(mmUserId,ppxiangmuId);
                int mmtotaljicaiyaoshu=ddService.findAllByXiangmuIdAndJicaiyao(ppxiangmuId).size();
                String mmShifoutijiao="";
                if("1".equals(ysrenyuanfenpei.get("issubmit").toString())){
                    mmShifoutijiao="是";
                }else {
                    mmShifoutijiao="否";
                }

                mmMap.put("username", mmUserName);
                mmMap.put("pinguishu", mmpinguishu+"/103");
                mmMap.put("jicaishu", mmjicaiyaoshu+"/"+mmtotaljicaiyaoshu);
                mmMap.put("shifoutijiao", mmShifoutijiao);
                mmJindulist.add(mmMap);

            }
            return JsonResult.getSuccessResult(mmJindulist);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("tongkeyaopin/getjindulist:error",e);
            return JsonResult.getErrorResult("tongkeyaopin/getjindulist:error " + e.getMessage());
        }
    }

    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("xiangmuid") String ppxiangmuId){
        try {

            return ddService.deleteByxiangmuId(ppxiangmuId)==true? JsonResult.getSuccessResult("删除材料成功")
                    : JsonResult.getErrorResult("删除材料失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("tongkeyaopin/delete:error",e);
            return JsonResult.getErrorResult("tongkeyaopin/delete:error " + e.getMessage());
        }
    }




}
